package user.view;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import db.CitiesManager;
import db.FlightsManage;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFormattedTextField;
import java.awt.Color;

public class FlightEdit extends JFrame {

	private JPanel contentPane;
	private JTextField id_text;
	private JTextField price_tf;
	private JTextField discount_tf;
	private JComboBox year_combox;
	private JComboBox month_combox;
	private JComboBox date_combox;
	private JComboBox hour_combox;
	private JComboBox min_combox;
	private JComboBox depart_combox;
	private JComboBox dest_combox;
	private JComboBox seat_combox;
	private JLabel lb;
	private int editid;
	private boolean isupdate;
	private JComboBox tickets_comobox;
	private static FlightEdit inst;
	private JLabel error_lb;
	public static FlightEdit getInstance()
	{
		if(inst==null)inst=new FlightEdit();
		return inst;
	}

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	private FlightEdit() {
		setTitle("AirLine Edit");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 522, 416);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblFlightId = new JLabel("Flight ID");
		lblFlightId.setBounds(40, 33, 73, 15);
		contentPane.add(lblFlightId);

		id_text = new JTextField();
		id_text.setEditable(false);
		id_text.setBounds(123, 30, 105, 21);
		contentPane.add(id_text);
		id_text.setColumns(10);

		JLabel lblDeparture = new JLabel("Departure");
		lblDeparture.setBounds(40, 74, 73, 15);
		contentPane.add(lblDeparture);

		depart_combox = new JComboBox();
		depart_combox.setBounds(123, 71, 105, 21);
		contentPane.add(depart_combox);

		JLabel lblDestination = new JLabel("Destination");
		lblDestination.setBounds(274, 74, 92, 15);
		contentPane.add(lblDestination);

		dest_combox = new JComboBox();
		dest_combox.setBounds(376, 71, 105, 21);
		contentPane.add(dest_combox);

		JLabel lblTakeOff = new JLabel("Take off");
		lblTakeOff.setBounds(42, 110, 71, 15);
		contentPane.add(lblTakeOff);

		year_combox = new JComboBox();
		year_combox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				changeDays();
			}
		});
		year_combox.setBounds(164, 107, 54, 21);
		contentPane.add(year_combox);

		JLabel lblY = new JLabel("Y");
		lblY.setBounds(123, 110, 54, 15);
		contentPane.add(lblY);

		JLabel lblM = new JLabel("M");
		lblM.setBounds(123, 142, 54, 15);
		contentPane.add(lblM);

		month_combox = new JComboBox();
		month_combox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				changeDays();
			}
		});
		month_combox.setBounds(164, 138, 54, 21);
		contentPane.add(month_combox);

		JLabel lblD = new JLabel("D");
		lblD.setBounds(123, 172, 54, 15);
		contentPane.add(lblD);

		date_combox = new JComboBox();
		date_combox.setBounds(164, 169, 54, 21);
		contentPane.add(date_combox);

		JLabel lblHour = new JLabel("Hour");
		lblHour.setBounds(274, 110, 54, 15);
		contentPane.add(lblHour);

		hour_combox = new JComboBox();
		hour_combox.setBounds(324, 107, 54, 21);
		contentPane.add(hour_combox);

		JLabel lblMinute = new JLabel("Minute");
		lblMinute.setBounds(274, 142, 54, 15);
		contentPane.add(lblMinute);

		min_combox = new JComboBox();
		min_combox.setBounds(324, 139, 54, 21);
		contentPane.add(min_combox);

		JLabel lblPrice = new JLabel("Price($)");
		lblPrice.setBounds(40, 200, 60, 15);
		contentPane.add(lblPrice);

		price_tf = new JTextField();
		price_tf.setText("1000");
		price_tf.setBounds(123, 197, 105, 21);
		contentPane.add(price_tf);
		price_tf.setColumns(10);

		JLabel lblDiscount = new JLabel("Seats");
		lblDiscount.setBounds(40, 240, 73, 15);
		contentPane.add(lblDiscount);

		seat_combox = new JComboBox();
		seat_combox.setBounds(123, 237, 54, 21);
		contentPane.add(seat_combox);

		JLabel lblDiscount_1 = new JLabel("Discount");
		lblDiscount_1.setBounds(40, 301, 54, 15);
		contentPane.add(lblDiscount_1);

		discount_tf = new JTextField();
		discount_tf.setText("0");
		discount_tf.setBounds(123, 298, 54, 21);
		contentPane.add(discount_tf);
		discount_tf.setColumns(10);

		lb = new JLabel("% off more than ");
		lb.setBounds(187, 301, 179, 15);
		contentPane.add(lb);

		tickets_comobox = new JComboBox();
		tickets_comobox.setBounds(296, 299, 38, 21);
		contentPane.add(tickets_comobox);

		JLabel lblTickets = new JLabel("tickets");
		lblTickets.setBounds(344, 302, 54, 15);
		contentPane.add(lblTickets);

		JSeparator separator = new JSeparator();
		separator.setBounds(38, 276, 443, 15);
		contentPane.add(separator);

		JButton btnSubmit = new JButton("submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				submit();
			}
		});
		btnSubmit.setBounds(211, 345, 93, 23);
		contentPane.add(btnSubmit);
		
		error_lb = new JLabel("");
		error_lb.setForeground(Color.RED);
		error_lb.setBounds(40, 10, 218, 15);
		contentPane.add(error_lb);
		initDateCombox();
	}
	private void submit()
	{
		double price;
		float discount;
		try{
			price=Double.parseDouble(price_tf.getText().trim());
			discount=Float.parseFloat(discount_tf.getText().trim());
		}catch (Exception e) {
			// TODO: handle exception
			error_lb.setText("wrong format!");
			return;
		}
		String time=hour_combox.getSelectedItem()+":"+min_combox.getSelectedItem()+":00";
		String date=year_combox.getSelectedItem()+"-"+month_combox.getSelectedItem()+"-"+date_combox.getSelectedItem();
		String departure=depart_combox.getSelectedItem().toString();
		String dest=dest_combox.getSelectedItem().toString();
		if(departure.equals(dest))
		{
			error_lb.setText("Departure equals with destination!");
			return;
		}
		if(isupdate){
			if(FlightsManage.updateAirline(editid,departure ,dest, price, seat_combox.getSelectedIndex()+1, time, date, discount, tickets_comobox.getSelectedIndex()+1))
			{
			FlightEdit.getInstance().setVisible(false);
			FlightsFrame.getInstance().flush();
			}
		}
		else {
			if(FlightsManage.insertAirline(depart_combox.getSelectedItem().toString(), dest_combox.getSelectedItem().toString(), price, seat_combox.getSelectedIndex()+1, time, date, discount, tickets_comobox.getSelectedIndex()+1))
			{
				{
					FlightEdit.getInstance().setVisible(false);
					FlightsFrame.getInstance().flush();
					}
			}
		};
	}
	private void changeDays() {
		date_combox.removeAllItems();
		Calendar c = Calendar.getInstance();
		c.set(year_combox.getSelectedIndex() + 2009, month_combox
				.getSelectedIndex(), 1);

		int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		for (int i = 1; i <= days; i++)
			date_combox.addItem(i);

		date_combox.setSelectedIndex(0);
	}

	private void initDateCombox() {
		for (int i = 2009; i < 2020; i++)
			year_combox.addItem(i);
		for (int i = 1; i <= 12; i++)
			month_combox.addItem(i);
		for (int i = 1; i <= 31; i++)
			date_combox.addItem(i);

		Calendar c = Calendar.getInstance();
		System.out.println(c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH)
				+ "-" + c.get(Calendar.DATE));

		year_combox.setSelectedIndex(c.get(Calendar.YEAR) - 2009);
		month_combox.setSelectedIndex(c.get(Calendar.MONTH));

		date_combox.setSelectedIndex(c.get(Calendar.DATE) - 1);
		for (int i = 0; i < 60; i++)
			min_combox.addItem(i);

		for (int i = 0; i < 24; i++)
			hour_combox.addItem(i);
		ArrayList<String[]> citie=CitiesManager.getCities();
		for (String[] strings : citie) {
			depart_combox.addItem(strings[0]);
			dest_combox.addItem(strings[0]);
		}
		for(int i=1;i<=300;i++)
			{seat_combox.addItem(i);
			tickets_comobox.addItem(i);
			}
	}
	public void setEdit(int id)
	{
		Vector<String> vector=FlightsManage.getFlights(id);
		setTitle("Airline Edit");
		isupdate=true;
		String[] y_m_d=vector.elementAt(1).split("-");
		String[] h_m=vector.elementAt(2).split(":");
		id_text.setText(id+"");
		editid=id;
		depart_combox.setSelectedItem(vector.elementAt(3));
		dest_combox.setSelectedItem(vector.elementAt(4));
		year_combox.setSelectedItem(Integer.parseInt(y_m_d[0]));
		month_combox.setSelectedItem(Integer.parseInt(y_m_d[1]));
		date_combox.setSelectedItem(Integer.parseInt(y_m_d[2]));
		
		hour_combox.setSelectedItem(Integer.parseInt(h_m[0]));
		min_combox.setSelectedItem(Integer.parseInt(h_m[1]));
		seat_combox.setSelectedItem(Integer.parseInt(vector.elementAt(5)));
		System.out.println(Integer.parseInt(vector.elementAt(5)));
		price_tf.setText(vector.elementAt(6));
		discount_tf.setText(vector.elementAt(7));
		tickets_comobox.setSelectedItem(Integer.parseInt(vector.elementAt(8)));
		System.out.println(vector.elementAt(8));
	}
	public void setInsertModel()
	{
		isupdate=false;
		id_text.setText("(auto)");
		setTitle("Airline Add");
		error_lb.setText("");
		discount_tf.setText("0");
		seat_combox.setSelectedIndex(149);
		price_tf.setText("1000.0");
		
	}
}
